package com.panda.utils;

import com.panda.common.ResourceHolder;
import com.panda.expand.ParamElementHandler;
import com.panda.git.PullUtils;
import com.panda.pojo.Context;

import java.util.List;
import java.util.Map;

//
public class PrePostUtils {

    public static void pre(Context context, ResourceHolder holder) {
        
        Map config = holder.getConfig();

        //angularjs和jQuery是必不可少的
        if (!config.containsKey("angularJs") || !config.containsKey("jQuery")) {
            throw new RuntimeException("angularJs和jQuery是不可缺少的配置");
        }


        context.getJsFiles().putAll(holder.getJs());
        context.getCssFiles().putAll(holder.getCss());


        //app名称
        String appName = (String) config.get("app.name");

        if (appName != null) {
            context.setAppName(appName);
        }

        List<Map> commands = holder.getCommands();

        for (Map map : commands) {


            //解析指令文件
            map.keySet().forEach(o -> {

                //指令
                String cmd = (String) o;

                Map vs = (Map) map.get(cmd);

                ParamElementHandler handler = new ParamElementHandler();

                //前缀
                handler.setPrefix(cmd);

                String key = (String) vs.get("key");

                if (key == null) {
                    throw new RuntimeException("定义 " + cmd + " 时未指定key");
                }

                handler.setKey(key);

                handler.setRegex((String) vs.get("regex"));

                handler.setValue((String) vs.get("value"));

                Boolean repetitive = (Boolean) vs.get("repetitive");

                if (repetitive != null) {
                    handler.setRepetitive(repetitive);
                }

                String multi = (String) vs.get("multi");

                if (multi != null) {
                    handler.setMulti(multi);
                }

                String split = (String) vs.get("split");

                if (split != null) {
                    handler.setSplit(split);
                }

                context.getCompositeElementSugar().addParamElementHandler(handler);

            });

        }

        //解析依赖

        List<Map> dependencies = holder.getDependencies();

        if (dependencies!=null)
        for (Map dependency : dependencies) {

            if (dependency!=null)
            for (Object o : dependency.keySet()) {

                String namespace = (String) o;

                String d = (String) dependency.get(namespace);

                String[] split = d.split("/");

                if (split.length != 3) {
                    throw new RuntimeException("依赖格式错误:" + d);
                }

                String group = split[0];
                String root = split[1];
                String version = split[2];

                //从后台拉取服务
                if (!PullUtils.pull(context, group, root, version, namespace)) {
                    throw new RuntimeException("处理依赖时出现错误: " + group + "/" + root + "/" + version);
                }

            }

        }

        //解析服务
        ServiceUtils.handle(context, holder.getServiceTexts());

        //解析组件
        ComponentUtils.handle(context, holder.getComponentTexts());

    }


}
